package eu.log.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

class Helper {
	/*
	 * 获取数据库连接
	 */
	public static Connection getConnection(String uri) {
		try {
			Class.forName("smallsql.database.SSDriver");
			return DriverManager.getConnection(uri);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 创建数据库
	 * 
	 * @param db
	 */
	public static void createDatabase(String db) {
		Connection conn = getConnection("jdbc:smallsql");
		try {
			conn.createStatement().execute("create database " + db);
		} catch (SQLException e) {
		}
	}

	/**
	 * 创建数据表
	 * 
	 * @param db
	 *            数据库名称
	 * @param table
	 *            数据表名称
	 */
	public static void createTable(String db, String table) {
		Connection conn = getConnection("jdbc:smallsql:" + db);
		String sql = "create table " + table
				+ " (clientIdentity VARCHAR(500) null," +
				"event text not null," +
				"time TIMESTAMP not null," +
				"rank VARCHAR(20) not null)";
		try {
			conn.createStatement().execute(sql);
		} catch (SQLException e) {
		}
	}
	
}
